<?php
/**
 * Created by PhpStorm.
 * User: mj
 * Date: 17-6-7
 * Time: 下午1:40
 */

namespace App\Winged\Auth;

use App\Winged\Auth\Traits\AdminPermission;
use App\Winged\Auth\Traits\Authenticatable;
use App\Winged\Auth\Traits\ModelWrite;
use Encore\Admin\Traits\AdminBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

/**
 * Class Admin
 *
 * @package App\Winged\Auth
 */
class Administrator extends Model implements \Illuminate\Contracts\Auth\Authenticatable
{
    /**
     * @var 登录token
     */
    private $token;

    /**
     * @var
     */
    public $id;

    /**
     * @var
     */
    public $status;

    /**
     * @var 等级
     */
    public $grade;

    /**
     * @var 医生姓名
     */
    public $name;

    /**
     * @var knwon
     */
    public $post;

    /**
     * @var  医生职称
     */
    public $jobTitle;

    /**
     * @var 医院id
     */
    public $hospitalId;

    /**
     * @var 科室id
     */
    public $departmentId;

    /**
     * @var 头像id
     */
    public $headImageId;

    /**
     * @var 手机号，其实就是用户名
     */
    public $mobile;

    /**
     * @var 密码，一般不显示在前端
     */
    private $password;

    /**
     * @var known
     */
    public $authentication;

    /**
     * @var array 个人属性 ，包括权限 角色 医院信息，科室信息
     */
    protected $properties;

    public $username;

    /*  以下字段不知道啥意思，暂时没啥用 */

    public $authenticated;

    public $accountNonExpired;

    public $accountNonLocked;

    public $credentialsNonExpired;

    public $authorities;

    public $enabled;

    public static $requiresUserInfo = false;

    public static $userInfo = null;


    use AdminPermission , Authenticatable ,AdminBuilder ,ModelWrite;


    public $fillable = [
        'id','token','status','grade','name','post','jobTitle','hospitalId','enabled',
        'departmentId','headImageId','mobile','password','authentication','properties','username',
        'authenticated','accountNonExpired','accountNonLocked','credentialsNonExpired','authorities',
        'doctor_info','authorities'
    ];


    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
    }

    /*
     * 向页面中写入当前用户的级别、id以及姓名信息
     */
    public function requireBasicUserInfo($fields = [])
    {
        $fields = array_merge(['id', 'name','doctor_info'] , $fields);
        $this->requireUserInfo($fields);
    }


    /**
     * 根据指定的字段列表，向页面中写入当前用户的信息，前端代码可通过LA.userInfo获取到指定的信息
     */
    public function requireUserInfo($fields)
    {
        $data = [];
        $attr = $this->getAttributes() ;
        foreach ($fields as $key => $val){
            $data ["info." . $val] = data_get($attr , $val);
        }
        if (!empty($data)){
            $this->userInfo = json_encode($data);
            $this->requiresUserInfo = true;
        }
    }

    public function getName()
    {
        $info = $this->getAttribute('doctor_info');
        return $info['name'] ?? null ;
    }

    public function getDepartmentName()
    {
        $info = $this->getAttribute('doctor_info');
        return empty($info['department_name']) ? "" : "所属科室：" . $info['department_name'] ;
    }
}
